﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using THSERP.BaseProvider;
using THSLib;
using THSERP.BusinessEntities;
namespace THSERP.Modules.POS
{
    public partial class guiChangeLocation : THSERPScreen
    {
        DataSet dsBRPOS = null;
        protected int _orglocationID;
        protected int _newlocationID;
        public int OrgLocation
        {
            get { return _orglocationID; }
            set { _orglocationID = value; }
        }
       
        public int NewLocation
        {
            get { return _newlocationID; }
            set { _newlocationID = value; }
        }
        BRPOSsController objBRPOSsController = new BRPOSsController();
        public guiChangeLocation(DataSet ds,int LocationID)
        {
            this.dsBRPOS = ds;
            this.OrgLocation = LocationID;
            InitializeComponent();
         
            
        }

        private void guiChangeLocation_Load(object sender, EventArgs e)
        {
            
            InitialNewLocation();
            InitialDataToBRPOs();
            //initial POSID reponsitory
            ths_lkePOSParent.DataSource = dsBRPOS.Tables[0];
            ths_lkePOSIDChild.DataSource = dsBRPOS.Tables[0];
        }
        public void InitialDataToBRPOs()
        {
            List<BRPOSsInfo> lst = new List<BRPOSsInfo>();
            foreach (DataRow row in dsBRPOS.Tables[0].Rows)
            {
                BRPOSsInfo objBRPOSsInfo = (BRPOSsInfo)objBRPOSsController.GetObjectFromDataRow(row);
                if (objBRPOSsInfo != null)
                {
                    lst.Add(objBRPOSsInfo);
                }
            }
            fld_dgcPOSs.DataSource = lst;
        }
        private void InitialDataToBRPOSItems(int BRPOSID)
        {
            List<BRPOSItemsInfo> list = new List<BRPOSItemsInfo>();

            BRPOSItemsController objBRPOSItemsController = new BRPOSItemsController();
            ICProductsController objICProductsController = new ICProductsController();
            THSList<ICProductsInfo> lstProducts = new THSList<ICProductsInfo>();
            //initial lookupedit
            
            
           
                DataSet ds = objBRPOSItemsController.GetAllDataByForeignColumn("FK_BRPOSID", BRPOSID);
                foreach (DataRow rowItem in ds.Tables[0].Rows)
                {
                    BRPOSItemsInfo objBRPOSItemsInfo = (BRPOSItemsInfo)objBRPOSItemsController.GetObjectFromDataRow(rowItem);
                    if (objBRPOSItemsInfo != null)
                    {
                        ICProductsInfo objICproductsInfo = (ICProductsInfo)objICProductsController.GetObjectByID(objBRPOSItemsInfo.FK_ICProductID);
                        if (objICproductsInfo != null)
                        {
                            if (!lstProducts.Exists("ICProductID", objICproductsInfo.ICProductID))
                                lstProducts.Add(objICproductsInfo);
                        }
                       
                        list.Add(objBRPOSItemsInfo);
                    }
                }

            
            //reponsitory GELocation
            List<GELocationsInfo> listLocation = new List<GELocationsInfo>();
            GELocationsInfo objLocationsInfo = (GELocationsInfo)new GELocationsController().GetObjectByID(OrgLocation);
            listLocation.Add(objLocationsInfo);
            thsRepositoryLookupEdit2.DataSource = listLocation;
            //Reponsitory Products
            thsRepositoryLookupEdit3.DataSource = lstProducts;
            fld_dgcPOSItems.DataSource = list;
            fld_dgvPOSItems.ExpandAllGroups();
        }
       
        private void InitialNewLocation()
        {
            DataSet dsGELocations = new GELocationsController().GetLocationByStatus(BusinessObject.DefaultStatus);
            DataSet dsAllLocation = new GELocationsController().GetAllObjects();
            List<GELocationsInfo> listGELocations = new List<GELocationsInfo>();
            foreach (DataRow row in dsGELocations.Tables[0].Rows)
            {
                GELocationsInfo obj = (GELocationsInfo)new GELocationsController().GetObjectFromDataRow(row);
                if(obj.GELocationParentID>0)
                    listGELocations.Add(obj);
            }
            //reponsitory datasource
            lkeGELocationParentID.DataSource = dsAllLocation.Tables[0];
            //Grid soucre
            fld_dgcLocation.DataSource = listGELocations;
            fld_dgvLocations.ExpandAllGroups();
        }

        
        private void fld_btnChange_Click(object sender, EventArgs e)
        {
            if (NewLocation == 0)
            {
                MessageBox.Show("Chưa chọn bàn chuyển đến! Vui lòng kiểm tra lại.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //get locationName by id
            String NewLocationName = new GELocationsController().GetObjectNameByID(NewLocation); 
            BRPOSLocationsController objBRPOSLocationsController = new BRPOSLocationsController();
            BRPOSItemsController objBRPOSItemsController = new BRPOSItemsController();
            BRPOSsController objBRPOSsController = new BRPOSsController();
            List<BRPOSLocationsInfo> lstPOSLocation = new List<BRPOSLocationsInfo>();
            List<BRPOSsInfo> lstBRPOSs = new List<BRPOSsInfo>();
            bool IsChange = false;
            foreach (BRPOSsInfo obj in (List<BRPOSsInfo>)fld_dgvPOSs.DataSource)
            {
                if (obj.AASelected)
                    lstBRPOSs.Add(obj);
            }
            if (lstBRPOSs.Count == 0)
            {
                MessageBox.Show("Chưa chọn Bill để chuyển sang bàn khác! Vui lòng kiểm tra lại.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            foreach (BRPOSsInfo obj in lstBRPOSs)
            {
                //change Location BRPOSLocation
                DataSet dsPOSLocations = objBRPOSLocationsController.GetByPOSIDAndLocationID(obj.BRPOSID, OrgLocation);
                foreach (DataRow row in dsPOSLocations.Tables[0].Rows)
                {
                    BRPOSLocationsInfo objBRPOSLocationsInfo = (BRPOSLocationsInfo)objBRPOSLocationsController.GetObjectFromDataRow(row);
                    objBRPOSLocationsInfo.FK_GELocationID = NewLocation;
                    objBRPOSLocationsInfo.AAUpdatedDate = DateTime.Now;
                    objBRPOSLocationsInfo.AAUpdatedUser = THSApp.CurrentUser;
                    objBRPOSLocationsController.UpdateObject(objBRPOSLocationsInfo);
                    IsChange = true;
                }
                //change Location BRPOSItems
                if (IsChange)
                {
                    //update main object No to new Location
                    obj.BRPOSNo = GetNewMainObjectNo(obj, NewLocationName);
                    obj.AAUpdatedDate = DateTime.Now;
                    obj.AAUpdatedUser = THSApp.CurrentUser;
                    objBRPOSsController.UpdateObject(obj);
                    //update newlocation to busy
                    ((POSModule)this.Module).UpdateLocationToBusy(NewLocation);
                    //update Location stus status 
                    ((POSModule)this.Module).UpdateLocationStatus(OrgLocation);
                   
                }
            }
            if (IsChange)
            {
                MessageBox.Show("Đã chuyển bàn!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
                MessageBox.Show("Chuyển bàn không thành công! Vui lòng kiểm tra lại.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
           
        }
        public String GetNewMainObjectNo(BRPOSsInfo objBRPOSsInfo,String NewLocationName)
        {
            String originalNo = objBRPOSsInfo.BRPOSNo;
            String NewObjectNo = NewLocationName;
            if (originalNo.Contains("/"))
            {
                int index = originalNo.IndexOf("/");
                NewObjectNo += originalNo.Substring(index, originalNo.Length-index);
            }
            else
                NewObjectNo += String.Format("/{0}", objBRPOSsInfo.BRPOSNo);
            return NewObjectNo;
        }
        private void fld_dgvPOSs_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            BRPOSsInfo objBRPOSsInfo = (BRPOSsInfo)fld_dgvPOSs.GetRow(e.FocusedRowHandle);
            if (objBRPOSsInfo != null)
                InitialDataToBRPOSItems(objBRPOSsInfo.BRPOSID);
        }

        private void fld_dgvPOSs_Click(object sender, EventArgs e)
        {

        }

        private void fld_dgvPOSs_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            BRPOSsInfo objBRPOSsInfo = (BRPOSsInfo)fld_dgvPOSs.GetRow(e.RowHandle);
            if (objBRPOSsInfo != null)
                InitialDataToBRPOSItems(objBRPOSsInfo.BRPOSID);
        }

        private void fld_trlGELocations_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (e.Node.HasChildren)
                NewLocation = 0;
            else
                NewLocation = Convert.ToInt32(e.Node["GELocationID"].ToString());
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fld_dgvLocations_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            List<GELocationsInfo> lst =new List<GELocationsInfo>();
            lst = (List<GELocationsInfo>)fld_dgvLocations.DataSource;
            if (Convert.ToBoolean(e.Value))
            {
                NewLocation = Convert.ToInt32(fld_dgvLocations.GetRowCellValue(e.RowHandle, colGELocationID));
                for (int i = 0; i <= fld_dgvLocations.RowCount; i++)
                {
                    if (i != e.RowHandle)
                    {
                        fld_dgvLocations.SetRowCellValue(i, colAASelected, false);
                    }
                }
            }
            fld_dgvLocations.RefreshData();
        }
    }
}